package com.hrcx.mystery.controller;

import com.alibaba.fastjson.JSONObject;
import com.hrcx.mystery.common.CommonPage;
import com.hrcx.mystery.common.CommonResult;
import com.hrcx.mystery.common.PageParamRequest;
import com.hrcx.mystery.dto.TasksSearchDto;
import com.hrcx.mystery.pojo.EbMysteryScheduleJob;
import com.hrcx.mystery.service.EbMysteryScheduleJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <h1>动态定时任务</h1>
 * <p>
 * 主要应用在奖池周期累积重置、活动自动开启/结束
 *
 * @author LinXiPeng
 * @className TbTaskController
 * @date 2021/12/13 17:43
 * Version 1.0
 */
@RestController
@Api(tags = {"盲盒定时任务"})
@RequestMapping("/tbTask/")
public class TbTaskController {
    @Resource
    private EbMysteryScheduleJobService scheduleJobService;

    /**
     * 创建任务
     */
    @ApiOperation("创建一个定时任务")
    @RequestMapping(value = "createJob", method = RequestMethod.POST)
    public CommonResult<JSONObject> createJob(@RequestBody EbMysteryScheduleJob scheduleJob) {
        return scheduleJobService.createJob(scheduleJob) ? CommonResult.success("创建成功") : CommonResult.failed("创建失败");
    }

    /**
     * 更新任务
     */
    @ApiOperation("更新定时任务")
    @RequestMapping(value = "updateJob", method = RequestMethod.PUT)
    public CommonResult<JSONObject> updateJob(@RequestBody EbMysteryScheduleJob scheduleJob) {
        return scheduleJobService.updateJob(scheduleJob) ? CommonResult.success("修改成功") : CommonResult.failed("修改失败");
    }

    /**
     * 立即执行任务(执行一次)
     */
    @ApiOperation("立即执行一个任务")
    @RequestMapping(value = "runJob", method = RequestMethod.POST)
    public CommonResult<JSONObject> runJob(@RequestParam Long jid) {
        return scheduleJobService.runById(jid) ? CommonResult.success("执行成功") : CommonResult.failed("执行失败");
    }

    /**
     * 恢复任务
     */
    @ApiOperation("恢复被停用的定时任务")
    @RequestMapping(value = "resumeJob", method = RequestMethod.POST)
    public CommonResult<JSONObject> resumeJob(@RequestParam Long jid) {
        return scheduleJobService.resumeJob(jid) ? CommonResult.success("恢复成功") : CommonResult.failed("恢复失败");
    }

    /**
     * 停用任务
     */
    @ApiOperation("停用定时任务")
    @RequestMapping(value = "pauseJob", method = RequestMethod.POST)
    public CommonResult<JSONObject> pauseJob(@RequestParam Long jid) {
        return scheduleJobService.pauseJob(jid) ? CommonResult.success("停用成功") : CommonResult.failed("停用失败");
    }

    /**
     * 删除任务
     */
    @ApiOperation("删除定时任务")
    @RequestMapping(value = "deleteJob", method = RequestMethod.DELETE)
    public CommonResult<JSONObject> deleteJob(@RequestParam Long jid) {
        return scheduleJobService.deleteJob(jid) ? CommonResult.success("删除成功") : CommonResult.failed("删除失败");
    }

    /**
     * 查看当前所有任务的状态
     */
    @ApiOperation("分页查询所有定时任务")
    @RequestMapping(value = "jobList", method = RequestMethod.GET)
    public CommonResult<CommonPage<EbMysteryScheduleJob>> getTaskList(@Validated TasksSearchDto tasksSearchDto,
                                                                      @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(scheduleJobService.getTaskList(tasksSearchDto, pageParamRequest)));
    }
}